package com.chen.positioning.util;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 公共返回Json具体实现方法
 *
 * @author halo
 * @create 2019-08-02 15:29
 */
@Service
public class CommonJsonReturnMessage {
    private static final Logger logger = LoggerFactory.getLogger(CommonJsonReturnMessage.class);

    /**
     * 成功返回时的方法
     *
     * @param object
     * @return
     * @throws Exception
     */
    public String successReturn(String returnMessage, Object object) throws Exception {
        CommonJsonReturn commonJsonReturn = new CommonJsonReturn();
        commonJsonReturn.setStatus(1);
        commonJsonReturn.setCode("0000");
        commonJsonReturn.setMessage(returnMessage);
        commonJsonReturn.setObject(object);
        logger.info("successReturn ==" + commonJsonReturn.toString());
        return JSONObject.toJSON(commonJsonReturn).toString();
    }

    /**
     * 失败返回时的方法
     *
     * @param object
     * @return
     * @throws Exception
     */
    public String failReturn(String returnMessage, Object object) {
        CommonJsonReturn commonJsonReturn = new CommonJsonReturn();
        commonJsonReturn.setStatus(2);
        commonJsonReturn.setCode("1111");
        commonJsonReturn.setMessage(returnMessage);
        commonJsonReturn.setObject(object);
        logger.error("failReturn ==" + commonJsonReturn.toString());
        logger.info(JSONObject.toJSON(commonJsonReturn).toString());
        return JSONObject.toJSON(commonJsonReturn).toString();
    }

    /*public static void main(String[] args) {
        CommonJsonReturn commonJsonReturn = new CommonJsonReturn();
        commonJsonReturn.setStatus(2);
        commonJsonReturn.setCode("1111");
        commonJsonReturn.setMessage("我是一個Message");
        commonJsonReturn.setObject("ddsads3213213321");
        logger.info(JSONObject.toJSON(commonJsonReturn).toString());
    }*/
}
